<?php

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT . 'controller/form.inc.php';
require_once WACT_ROOT . 'validation/rules/match.inc.php';
require_once WACT_ROOT . 'validation/rules/member.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';
require_once APP_ROOT . 'model/common/event.php';
require_once APP_ROOT . 'model/common/categories.php';
require_once APP_ROOT . 'model/common/session.php';
require_once APP_ROOT . 'model/common/form.php';
require_once APP_ROOT.'include/inc.config.php';


// event view form
class EventViewFormController extends FormController {

	var $admin_user = false;
	var $staff_user = false;
	var $view; // current view

	function setUserType($admin_user=false, $staff_user = false) {
		$this->admin_user = $admin_user;
        $this->staff_user = $staff_user;
	}

    function setView(&$view) {
    	$this->view = $view;
    }


	 function EventViewFormController() {
     	$this->addChild('deny', new ButtonController(new Delegate($this, 'deny'))); // deny
        $this->addChild('publish', new ButtonController(new Delegate($this, 'publish'))); // publish
        $this->addChild('viewnext', new ButtonController(new Delegate($this, 'viewnext'))); // view next

        $this->addChild('events', new ButtonController(new Delegate($this, 'back'))); // clean
        $this->setDefaultChild('events');

        $this->registerOnLoadListener(new Delegate($this, 'load'));
        //$this->registerOnActivateListener(new Delegate($this, 'check'));

        $this->addView('success', WACT_DEFAULT_VIEW);
        $this->addView('notfound', WACT_DEFAULT_VIEW);
        $this->addView('delete', new Handle('RedirectView', array('/delete')));
        $this->addView('publish', new Handle('RedirectView', array('/publish')));
        $this->addView('deny', new Handle('RedirectView', array('/deny')));
        $this->addView('viewnext', new Handle('RedirectView', array('/viewnext')));
        $this->addView('this', WACT_DEFAULT_VIEW);
        $this->addView('index', new Handle('RedirectView', array('/')));

	 }




    function publish(&$source, &$request, &$responseModel) {
        $event_id = $responseModel->get('event_id');
        if ( $event_id ) {
           Event::publishEvent($event_id);
           $this->loadEvent(&$responseModel, $event_id);
        }
        //$responseModel->set('denied',0);
        //$responseModel->remove('denied_show');
        //$responseModel->set('published',1);
        //$responseModel->set('published_show',1);
    	return 'this';
    }

    function deny(&$source, &$request, &$responseModel) {
        $event_id = $responseModel->get('event_id');
        if ( $event_id ) {
           Event::denyEvent($event_id);
           $this->loadEvent(&$responseModel, $event_id);
        }
        //$responseModel->set('denied',1);
        //$responseModel->set('denied_show',1);
        //$responseModel->set('published',0);
        //$responseModel->remove('published_show');
    	return 'this';
    }

    function viewnext(&$source, &$request, &$responseModel) {
    	// TO DO: find the event_id of the next event
    	$event_id =  $responseModel->get('event_id');
        //$event_id = 23;
        if (!$event_id) $event_id = $request->getParameter('event_id');

        $next_event =& Event::getNextNewEvent($event_id);
        if (is_null( $next_event ) ) $next_event_id = $event_id;
        else $next_event_id = $next_event->get('event_id');
        $responseModel->set('event_id', $next_event_id);

        if ($next_event_id) {
        	$this->loadEvent($responseModel, $next_event_id);
        }
        return 'this';

    }

    function back(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'index';
    }

    function loadEvent(&$responseModel, $event_id)   {
        	$record =& Event::getRecord($event_id);
        	if (is_null($record)) {
            	return 'notfound';
        	}
            $responseModel->import($record->export());

            //load width and height of the image
            //$photo_url = $responseModel->get('small_picture');
            //list($width, $height, $img_type, $attr) = @getimagesize(HOST.'/'.$photo_url);
            //$responseModel->set('image_width', $width);
            //$responseModel->set('image_height', $height);

            // Author
            $author_show = User::getFullUserName($responseModel);
            $responseModel->set('author_show',$author_show);

            //category
            //$category1 = $responseModel->get('category1');
            //$category2 = $responseModel->get('category2');
            //if ( $category2 && $category1 ) $category_show = $category1.'-'.$category2;
            //else if (  $category2 ) $category_show = $category2;
            //else if (  $category1 ) $category_show = $category1;
            //$responseModel->set('category_show',$category_show);

            //Status
            $published = $responseModel->get('published');
            $denied = $responseModel->get('denied');
            if ($published) $status_show = 'Published';
            else if($denied) $status_show = 'Denied';
            else if(!$denied) $status_show = 'New/Not Published';
            $responseModel->set('status_show',$status_show);

            // public url
            $public_url = Event::getPublicURLByID($event_id);
            $responseModel->set('public_url',$public_url);

            //premium
            $premium = $responseModel->get('premium');
            if ($premium) $premium_show = 'Premium';
            else $premium_show = 'Free';
            $responseModel->set('premium_show',$premium_show);

            if ($denied) $denied_show = 1;
            if ($published) $published_show = 1;
            $responseModel->set('denied_show',$denied_show);
            $responseModel->set('published_show',$published_show);


            //$next_event =& Event::getNextNewEvent($event_id);
        	//if (!is_null( $next_event ) ) $next_available = 1;
            //if ($next_available) $responseModel->set('next_available',$next_available);

            $occurance_show = Event::getEventOccurance($responseModel);
            $responseModel->set('occurance_show',$occurance_show);
    }

    function load(&$source, &$request, &$responseModel) {
        $event_id = $responseModel->get('event_id');
     	if (!$event_id) $event_id = $request->getParameter('event_id');
     	//print 'Load called';
     	if ($event_id) {
        	$this->loadEvent($responseModel, $event_id);
        }
        return 'this';
     }

     //prepare the form view
     function prepare(&$source, &$request, &$responseModel, &$template) {
     	//print 'Prepare form...';
     	if ($this->admin_user) {
     		$responseModel->set('admin_user',1);
     	}
     	if ($this->staff_user) $responseModel->set('staff_user',1);

     	//$template =& $view->Template;

     }

}

?>